﻿package com.PlantVsZombies.game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.PlantVsZombies.constant.GameVariable;
import com.PlantVsZombies.constant.ZombieType;
import com.PlantVsZombies.db.dao.PassDao;
import com.PlantVsZombies.db.model.Pass;
import com.PlantVsZombies.pass.PassBuilder;
import com.PlantVsZombies.pass.PassGroup;
import com.PlantVsZombies.pass.PassMeta;
import com.PlantVsZombies.ui.GamePanel;

/**
 * 自定义关卡界面
 * 
 * @author wangdongdong
 *
 */
public class PassCustomPanel extends JPanel {

	private JLabel jLabel0;
	private JButton jButton0;
	private JLabel jLabel1;
	private JComboBox jComboBox0;
	private JLabel jLabel2;
	private JSpinner jSpinner0;
	private JLabel jLabel3;
	private JComboBox jComboBox2;
	private JLabel jLabel4;
	private JSpinner jSpinner1;
	private List<PassMeta> metas;
	/** 总关卡数 */
	private int totalPass = 1;
	/** 当前关卡 */
	private int currentPass = 1;
	/** 总波数 */
	private int totalGroup = 1;
	/** 当前波数 */
	private int currentGroup = 1;
	/** 当前波僵尸数量 */
	private int groupCount = 1;
	private JButton jButton1;
	private JSpinner jSpinner2;
	/** 可选择僵尸集合 */
	private List<JCheckBox> zombieSelectList = new ArrayList<>();
	private static final int ZOMBIE_SELECT_WIDTH = 120;
	private static final int ZOMBIE_SELECT_HEIGHT = 30;
	private static final int ZOMBIE_VERTICAL_DISTANCE = 50;
	private JScrollPane jScrollPane1;
	private JButton jButton2;
	private JButton jButton3;

	private PassDao passDao = new PassDao();
	private JLabel jLabel5;
	private JTextField jTextField0;
	private JFrame frame;

	private void initComponents() {
		setLayout(null);
		add(getJButton2());
		add(getJButton3());
		add(getJScrollPane1());
		add(getJLabel1());
		add(getJComboBox0());
		add(getJLabel2());
		add(getJLabel0());
		add(getJSpinner0());
		add(getJLabel3());
		add(getJComboBox2());
		add(getJLabel4());
		add(getJSpinner1());
		add(getJButton1());
		add(getJButton0());
		add(getJSpinner2());
		add(getJLabel5());
		add(getJTextField0());
		setSize(441, 500);
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
			jTextField0.setText(GameVariable.temp_pass_name != null ? GameVariable.temp_pass_name : "");
			jTextField0.setBounds(130, 20, 228, 22);
		}
		return jTextField0;
	}

	private JLabel getJLabel5() {
		if (jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("关卡名称：");
			jLabel5.setBounds(61, 23, 65, 18);
		}
		return jLabel5;
	}

	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("保存关卡");
			jButton3.setBounds(306, 403, 100, 28);
			jButton3.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					if (PassBuilder.validatePass(metas)) {
						String name = jTextField0.getText().trim();
						String data = PassBuilder.passToJsonString(metas);
						Pass pass = new Pass();
						pass.setData(data);
						pass.setName(name);
						pass.setPlayer(GameVariable.player);
						pass.setTime(new Date());
						passDao.insertPass(pass);
						
						JOptionPane.showConfirmDialog(null, "保存成功", "温馨提示!", JOptionPane.DEFAULT_OPTION);
						GameVariable.list = metas;
						
						MainFrame.P.remove(MainFrame.m);
						
						Menu.menu.stop();
						MainFrame.p1 = new GamePanel();
						MainFrame.p1.Action();
						MainFrame.P.add(MainFrame.p1);
						MainFrame.P.setVisible(true);
						MainFrame.P.setFocusable(true);
						MainFrame.p1.requestFocus();
						MainFrame.P.repaint();
						
						frame.dispose();
					} else {
						JOptionPane.showConfirmDialog(null, "部分关卡信息不完整，请补充完整", "温馨提示!", JOptionPane.DEFAULT_OPTION);
					}
				}
			});
		}
		return jButton3;
	}

	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("确认");
			jButton2.setBounds(204, 403, 81, 28);
			jButton2.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					if (metas == null) {
						JOptionPane.showConfirmDialog(null, "请确认总关卡数!", "温馨提示!", JOptionPane.DEFAULT_OPTION);
						return;
					}
					System.out.println("总关卡：" + totalPass);
					System.out.println("当前关卡:" + currentPass);
					System.out.println("僵尸波数：" + totalGroup);
					System.out.println("当前波数：" + currentGroup);
					System.out.println("当前波僵尸数:" + groupCount);
					PassMeta meta = metas.get(currentPass - 1);
					meta.setGroupCount(totalGroup);
					if (meta.getGroups() == null) {
						List<PassGroup> groups = new ArrayList<PassGroup>(totalGroup);
						for (int i = 0; i < totalGroup; i++) {
							groups.add(new PassGroup());
						}
						meta.setGroups(groups);
					}
					PassGroup group = meta.getGroups().size() < currentGroup ? new PassGroup()
							: meta.getGroups().get(currentGroup - 1);
					int[] zombies = new int[groupCount];
					int j = 0;
					for (int i = 0; i < zombieSelectList.size(); i++) {
						JCheckBox box = zombieSelectList.get(i);
						if (box.isSelected()) {
							if (j == groupCount) {
								JOptionPane.showConfirmDialog(null, "请选择" + groupCount + "个僵尸!", "温馨提示!",
										JOptionPane.DEFAULT_OPTION);
								return;
							}
							zombies[j++] = ZombieType.valueOfName(box.getText()).getCode();
						}
					}
					if (j < groupCount) {
						JOptionPane.showConfirmDialog(null, "请选择" + groupCount + "个僵尸!", "温馨提示!",
								JOptionPane.DEFAULT_OPTION);
						return;
					}
					group.setZombies(zombies);
					if (meta.getGroups().size() < currentGroup)
						meta.getGroups().add(group);
					System.out.println(PassBuilder.passToJsonString(metas));
				}
			});
		}
		return jButton2;
	}

	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane(getZombieSelectPanel());
			jScrollPane1.setBounds(25, 215, 380, 171);
		}
		return jScrollPane1;
	}

	/**
	 * 僵尸选择显示面板
	 * 
	 * @return
	 */
	private JPanel getZombieSelectPanel() {
		JPanel panel = new ZombiePanel();
		panel.setSize(300, ZombieType.values().length);
		panel.setPreferredSize(ZombieType.getDimension());
		panel.setLayout(null);
		for (ZombieType zombie : ZombieType.values()) {
			JCheckBox zombieJCheckBox = getZombieJCheckBox(zombie.getX(), zombie.getY(), zombie.getName());
			zombieSelectList.add(zombieJCheckBox);
			panel.add(zombieJCheckBox);
		}
		return panel;
	}
	
	class ZombiePanel extends JPanel {
		/*
		 * 此方法传入参数Graphics g
		 * 实现把僵尸画入面板
		 * 每次只画当前僵尸的静态图片：presetImage；
		 */
		public void drawImage(Graphics g, Image image, int x, int y){
			
				g.drawImage(image, x-20,y-100,100,100, null);
			//else
				//g.drawImage(Zombie_StaticVar.boomdie, this.getRow(),this.getCol(), null);
		}
		
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			for (ZombieType zombie : ZombieType.values()) {
				this.drawImage(g, zombie.getImage(), zombie.getX(), zombie.getY());
			}
			repaint();
		}
	}

	/**
	 * 获取僵尸选择框
	 * 
	 * @param x
	 * @param y
	 * @param name
	 * @return
	 */
	private JCheckBox getZombieJCheckBox(int x, int y, String name) {
		JCheckBox box = new JCheckBox();
		box.setText(name);
		box.setBounds(x, y, ZOMBIE_SELECT_WIDTH, ZOMBIE_SELECT_HEIGHT);
		box.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int count = 0;
				for (int i = 0; i < zombieSelectList.size(); i++) {
					JCheckBox box = zombieSelectList.get(i);
					if (box.isSelected()) {
						count++;
					}
				}
				if (count == groupCount) {
					for (int i = 0; i < zombieSelectList.size(); i++) {
						JCheckBox box = zombieSelectList.get(i);
						if (!box.isSelected()) {
							box.setEnabled(false);
						}
					}
				} else {
					for (int i = 0; i < zombieSelectList.size(); i++) {
						JCheckBox box = zombieSelectList.get(i);
						box.setEnabled(true);
					}
				}
			}
		});
		return box;
	}

	/**
	 * 总波数设置
	 * @return
	 */
	private JSpinner getJSpinner1() {
		if (jSpinner1 == null) {
			jSpinner1 = new JSpinner();
			jSpinner1.setModel(new SpinnerNumberModel(1, 1, 10, 1));
			jSpinner1.setBounds(301, 120, 59, 22);
			jSpinner1.addChangeListener(new ChangeListener() {

				public void stateChanged(ChangeEvent event) {
					totalGroup = (int) jSpinner1.getValue();
					if (metas.get(currentPass - 1).getGroups() == null) {
						List<PassGroup> groups = new ArrayList<>();
						for (int i = 0; i < totalGroup; i++) {
							groups.add(new PassGroup());
						}
						metas.get(currentPass - 1).setGroups(groups);
					} else {
						Integer metaTotalGroup = metas.get(currentPass - 1).getGroupCount();
						if (metaTotalGroup != null && metaTotalGroup < totalGroup) {
							for (int i = 0; i < totalGroup - metaTotalGroup; i++) {
								metas.get(currentPass - 1).getGroups().add(new PassGroup());
							}
						} else if (metaTotalGroup != null && metaTotalGroup > totalGroup) {
							metas.get(currentPass - 1).getGroups().remove(totalGroup);
							currentGroup = metaTotalGroup - 1;
							PassGroup passGroup = metas.get(currentPass - 1).getGroups().get(0);
							jSpinner2.setValue(passGroup.getCount() == 0 ? 1 : passGroup.getCount());
							if (passGroup.getCount() != 0) {
								int[] zombies = passGroup.getZombies();
								for (JCheckBox box : zombieSelectList) {
									box.setSelected(false);
									if (PassBuilder.isContain(zombies, ZombieType.valueOfName(box.getText()).getCode()))
										box.setSelected(true);
									else
										box.setEnabled(false);
								}
							}
						} else {
							for (int i = 0; i < totalGroup - (metaTotalGroup == null ? 0 : metaTotalGroup); i++) {
								metas.get(currentPass - 1).getGroups().add(new PassGroup());
							}
						}
					}
					Integer items[] = new Integer[totalGroup];
					for (int i = 0; i < totalGroup; i++) {
						items[i] = i + 1;
					}
					ComboBoxModel<Integer> model = new DefaultComboBoxModel<Integer>(items);
					jComboBox2.setModel(model);
				}
			});
		}
		return jSpinner1;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("修改");
			jButton1.setEnabled(false);
			jButton1.setBounds(299, 64, 62, 28);
			jButton1.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					jButton0.setEnabled(true);
					jSpinner0.setEnabled(true);
				}
			});
		}
		return jButton1;
	}

	private JSpinner getJSpinner2() {
		if (jSpinner2 == null) {
			jSpinner2 = new JSpinner();
			jSpinner2.setModel(new SpinnerNumberModel(1, 1, 7, 1));
			jSpinner2.setBounds(301, 166, 58, 22);
			jSpinner2.addChangeListener(new ChangeListener() {

				public void stateChanged(ChangeEvent event) {
					groupCount = (int) jSpinner2.getValue();
					for (int i = 0; i < zombieSelectList.size(); i++) {
						JCheckBox box = zombieSelectList.get(i);
						box.setEnabled(true);
					}
				}
			});
		}
		return jSpinner2;
	}

	private JLabel getJLabel4() {
		if (jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("僵尸数量：");
			jLabel4.setBounds(212, 167, 70, 18);
		}
		return jLabel4;
	}

	/**
	 * 当前波数
	 * @return
	 */
	private JComboBox getJComboBox2() {
		if (jComboBox2 == null) {
			jComboBox2 = new JComboBox();
			jComboBox2.setModel(new DefaultComboBoxModel(new Object[] { 1 }));
			jComboBox2.setDoubleBuffered(false);
			jComboBox2.setBorder(null);
			jComboBox2.setBounds(124, 160, 60, 27);
			jComboBox2.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					currentGroup = (int) jComboBox2.getSelectedItem();
					if (metas != null && metas.size() > 0) {
						PassMeta passMeta = metas.get(currentPass - 1);
						if (passMeta != null) {
							if (passMeta.getGroups().size() >= currentGroup) {
								PassGroup passGroup = passMeta.getGroups().get(currentGroup - 1);
								jSpinner2.setValue(passGroup.getCount() == 0 ? 1 : passGroup.getCount());
								if (passGroup.getCount() != 0) {
									int[] zombies = passGroup.getZombies();
									for (JCheckBox box : zombieSelectList) {
										box.setSelected(false);
										if (PassBuilder.isContain(zombies,
												ZombieType.valueOfName(box.getText()).getCode()))
											box.setSelected(true);
										else
											box.setEnabled(false);
									}
									return;
								}
							}
						}
					}
					jSpinner2.setValue(1);
					for (JCheckBox box : zombieSelectList) {
						box.setSelected(false);
						box.setEnabled(true);
					}
				}
			});
		}
		return jComboBox2;
	}

	private JLabel getJLabel3() {
		if (jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("当前波数：");
			jLabel3.setBounds(59, 166, 69, 18);
		}
		return jLabel3;
	}

	/**
	 * 总关卡数
	 * @return
	 */
	private JSpinner getJSpinner0() {
		if (jSpinner0 == null) {
			jSpinner0 = new JSpinner();
			jSpinner0.setModel(new SpinnerNumberModel(1, 1, 20, 1));
			jSpinner0.setBounds(127, 66, 56, 22);
			jSpinner0.addChangeListener(new ChangeListener() {

				public void stateChanged(ChangeEvent event) {
					totalPass = (int) jSpinner0.getValue();
					if (metas != null) {
						if (metas.size() < totalPass) {
							metas.add(new PassMeta());
						} else if (metas.size() > totalPass) {
							metas.remove(metas.size() - 1);
						}
					}
				}
			});
		}
		return jSpinner0;
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("僵尸波数：");
			jLabel2.setBounds(212, 121, 65, 18);
		}
		return jLabel2;
	}

	/**
	 * 当前关卡
	 * @return
	 */
	private JComboBox getJComboBox0() {
		if (jComboBox0 == null) {
			jComboBox0 = new JComboBox();
			jComboBox0.setModel(new DefaultComboBoxModel(new Object[] {}));
			jComboBox0.setDoubleBuffered(false);
			jComboBox0.setBorder(null);
			jComboBox0.setBounds(124, 115, 60, 27);
			jComboBox0.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					currentPass = (int) jComboBox0.getSelectedItem();
					PassMeta passMeta = metas.get(currentPass - 1);
					Integer count = passMeta.getGroupCount();
					if (count == null) {
						totalGroup = 1;
						jSpinner1.setValue(1);
						currentGroup = 1;
						jComboBox2.setSelectedItem(1);
						groupCount = 1;
						jSpinner2.setValue(1);
						for (JCheckBox box : zombieSelectList) {
							box.setSelected(false);
							box.setEnabled(true);
						}
					} else {
						totalGroup = count;
						jSpinner1.setValue(count);
						Integer items[] = new Integer[count];
						for (int i = 0; i < count; i++) {
							items[i] = i + 1;
						}
						ComboBoxModel<Integer> model = new DefaultComboBoxModel<Integer>(items);
						jComboBox2.setModel(model);
						PassGroup passGroup = passMeta.getGroups().get(0);
						if (passGroup == null) {
							jSpinner2.setValue(1);
							groupCount = 1;
							for (JCheckBox box : zombieSelectList) {
								box.setSelected(false);
								box.setEnabled(true);
							}
						} else {
							jSpinner2.setValue(passGroup.getCount() == 0 ? 1 : passGroup.getCount());
							if (passGroup.getCount() != 0) {
								int[] zombies = passGroup.getZombies();
								for (JCheckBox box : zombieSelectList) {
									box.setSelected(false);
									if (PassBuilder.isContain(zombies, ZombieType.valueOfName(box.getText()).getCode()))
										box.setSelected(true);
									else
										box.setEnabled(false);
								}
							}
						}
					}
					System.out.println(PassBuilder.passToJsonString(metas));
				}
			});
		}
		return jComboBox0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("当前关卡：");
			jLabel1.setBounds(58, 118, 65, 18);
		}
		return jLabel1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("确认");
			jButton0.setBounds(209, 64, 66, 28);
			jButton0.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					if (metas == null) {
						metas = new ArrayList<>(totalPass);
						for (int i = 0; i < totalPass; i++) {
							metas.add(new PassMeta());
						}
					}
					jButton0.setEnabled(false);
					jButton1.setEnabled(true);
					jSpinner0.setEnabled(false);
					Integer items[] = new Integer[totalPass];
					for (int i = 0; i < totalPass; i++) {
						items[i] = i + 1;
					}
					ComboBoxModel<Integer> model = new DefaultComboBoxModel<Integer>(items);
					jComboBox0.setModel(model);
				}
			});
		}
		return jButton0;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("总关卡数:");
			jLabel0.setBounds(60, 69, 70, 18);
		}
		return jLabel0;
	}

	public PassCustomPanel(JFrame frame) {
		this.frame = frame;
		initComponents();
		metas = GameVariable.temp_list;
		if (metas != null) {
			// 总关卡数回显
			totalPass = metas.size();
			jSpinner0.setValue(totalPass);
			Integer items[] = new Integer[totalPass];
			for (int i = 0; i < totalPass; i++) {
				items[i] = i + 1;
			}
			ComboBoxModel<Integer> model = new DefaultComboBoxModel<Integer>(items);
			jComboBox0.setModel(model);
			// 当前关卡回显 默认显示第一关
			jComboBox0.setSelectedItem(1);
			// 总波数回显
			totalGroup = metas.get(0).getGroups().size();
			jSpinner1.setValue(totalGroup);

			// 总僵尸数回显
			groupCount = metas.get(0).getGroups().get(0).getCount();
			jSpinner2.setValue(groupCount);

		}
	}
	
}
